dynamic class Man extends MovieClip
{
    var grav: Number = 1.2;
    var EF;
    var _name;
    var _parent;
    var _rotation;
    var _x;
    var _xmouse;
    var _xscale;
    var _y;
    var _ymouse;
    var beingHeld;
    var btn;
    var defFrame;
    var fallRecovarable;
    var gotoAndStop;
    var hArea;
    var hitByBat;
    var localToGlobal;
    var num;
    var oldPoses;
    var onLine;
    var pointNum;
    var pointsA;
    var speedFactor;
    var stopped;
    var walkSpeed;
    var xv;
    var yv;

    function Man()
    {
        super();
        this.defFrame = "running";
        this.speedFactor = 1;
        this.pointsA = this._parent._parent.pointsA;
        this.hitByBat = false;
    }

    function init(tSide, tLevel)
    {
        this.beingHeld = this.stopped = false;
        this.onLine = true;
        this.walkSpeed = 1 + tLevel / 10 + Math.random();
        if (tSide > 0) 
        {
            this.pointNum = 0;
        }
        else 
        {
            this.pointNum = this.pointsA.length - 1;
            this.walkSpeed = this.walkSpeed * -1;
            this._xscale = -100;
        }
        this.activateButton();
        this.placeME();
        this.EF = this.walkEF;
        this.num = Number(this._name.substr(3, this._name.length - 3));
    }

    function walkEF()
    {
        if (this.stopped) 
        {
            return;
        }
        this.pointNum = this.pointNum + this.walkSpeed * this.speedFactor;
        this.placeME();
        if (this.hArea.hitTest(this._parent._parent.ashes.hArea)) 
        {
            this._parent._parent.hitAshes(this.num);
        }
        this.checkTurn();
    }

    function fallEF()
    {
        this._x = this._x + this.xv;
        this._y = this._y + this.yv;
        this.yv = this.yv + this.grav;
        if (this._parent._parent.ground.hitTest(this._x, this._y, true)) 
        {
            this._rotation = 0;
            var __reg3 = this._parent._parent.findPathPoint(this._x);
            this._y = this._parent._parent.pointsA[__reg3].y;
            var __local0;
            if (this.yv < 10 && this.fallRecovarable) 
            {
                this.onLine = true;
                this.pointNum = this.findPathPoint();
                var __reg2 = this.pointsA.length / 2;
                if ((this.pointNum < __reg2 && this.walkSpeed < 0) || (this.pointNum > __reg2 && this.walkSpeed > 0)) 
                {
                    this.walkSpeed = this.walkSpeed * -1;
                    this._xscale = this._xscale * -1;
                }
                this._parent._parent.removeFlyingEnemy(this);
                this.EF = this.walkEF;
                __local0 = this.gotoAndStop(this.defFrame);
            }
            else 
            {
                if (this.fallRecovarable || Math.random() > 0.8) 
                {
                    this._parent._parent.hitOw();
                }
                this.fallRecovarable = false;
                if (this._x > 288) 
                {
                    if (this.xv < 4) 
                    {
                        this.xv = 4;
                    }
                }
                else if (this.xv > -4) 
                {
                    this.xv = -4;
                }
                this.yv = -2;
                __local0 = this.gotoAndStop("impact");
            }
        }
        if (this._x < -10 || this._x > 610 || this._y > 460) 
        {
            this._parent._parent.removeFlyingEnemy(this);
            this._parent._parent.killEnemy(this.num);
        }
    }

    function beingHeldEF()
    {
        var __reg3 = {x: this._xmouse, y: this._ymouse};
        this.localToGlobal(__reg3);
        this._x = this._parent._xmouse;
        this._y = this._parent._ymouse + 18;
        var __reg2 = this.regulateValues(this.oldPoses[0].x - this._x);
        this._rotation = (0 - __reg2) / 3;
        this.oldPoses.shift();
        this.oldPoses.push(new Point(this._x, this._y));
    }

    function hitAshesEF()
    {
        this._parent._parent.hitAshes(this.num);
    }

    function placeME()
    {
        this._x = this.pointsA[Math.round(this.pointNum)].x;
        this._y = this.pointsA[Math.round(this.pointNum)].y;
        if (this.pointsA[Math.round(this.pointNum)].x == undefined || this.pointsA[Math.round(this.pointNum)].x == NaN) 
        {
            trace("for fuck sake!");
        }
    }

    function checkTurn()
    {
        if ((this.pointNum <= 0 && this.walkSpeed < 0) || (this.pointNum >= this.pointsA.length - 1 && this.walkSpeed > 0)) 
        {
            this.walkSpeed = this.walkSpeed * -1;
            this._xscale = this._xscale * -1;
        }
    }

    function turn(turnOb)
    {
        if ((this.walkSpeed > 0 && this._x < turnOb._x) || (this.walkSpeed < 0 && this._x > turnOb._x)) 
        {
            this.walkSpeed = this.walkSpeed * -1;
            this._xscale = this._xscale * -1;
        }
    }

    function doHit()
    {
        this.xv = this.yv = -5;
    }

    function pickUp()
    {
        if (!this._parent._parent.cursorBusy && !this._parent._parent.paused) 
        {
            this.onLine = false;
            this.beingHeld = true;
            this.EF = this.beingHeldEF;
            this.oldPoses = [];
            var __reg2 = 0;
            while (__reg2 < 3) 
            {
                this.oldPoses.push(new Point(this._x, this._y));
                ++__reg2;
            }
            this._parent._parent.pickUp();
            this._rotation = 0;
        }
    }

    function drop()
    {
        if (this.beingHeld) 
        {
            this.xv = this.regulateValues(this._x - this.oldPoses[0].x) / 5;
            this.yv = this.regulateValues(this._y - this.oldPoses[0].y) / 5;
            this._rotation = Math.random() * 360;
            this.fallRecovarable = true;
            this._parent._parent.drop(this);
            this.EF = this.fallEF;
            this.beingHeld = false;
            if (this._x > 595) 
            {
                this._x = 595;
            }
            if (this._x < 5) 
            {
                this._x = 5;
            }
            if (this._y > 480) 
            {
                var __reg2 = this._parent._parent.findPathPoint(this._x);
                this._y = this._parent._parent.pointsA[__reg2].y;
            }
            return this.gotoAndStop("falling");
        }
        this._parent._parent.bgClick();
    }

    function knockDown(tv)
    {
        this.fallRecovarable = false;
        if (this.onLine) 
        {
            this.xv = this.yv = 0;
        }
        this.onLine = false;
        this.EF = this.fallEF;
    }

    function electrify()
    {
        SoundManager.startSound("thatgonnasting" + Math.floor(Math.random() * 2));
        SoundManager.startSound("Thunder");
        this.EF = null;
        this.onLine = false;
    }

    function finishElectocute()
    {
        this._parent._parent.killEnemy(this.num);
    }

    function activateButton()
    {
        this.btn.onPress = function ()
        {
            this._parent.pickUp();
        }
        ;
        this.btn.onRelease = this.btn.onReleaseOutside = function ()
        {
            this._parent.drop();
        }
        ;
    }

    function regulateValues(val)
    {
        if (val > 50) 
        {
            val = 50;
        }
        else if (val < -50) 
        {
            val = -50;
        }
        return val;
    }

    function findPathPoint()
    {
        var __reg4 = 1000;
        var __reg5 = undefined;
        var __reg2 = 0;
        while (__reg2 < this.pointsA.length) 
        {
            var __reg3 = Math.abs(this._x - this.pointsA[__reg2].x);
            if (__reg3 < __reg4) 
            {
                __reg5 = __reg2;
                __reg4 = __reg3;
            }
            ++__reg2;
        }
        return __reg5;
    }

    function finishImpact()
    {
        this._rotation = Math.random() * 360;
    }

    function makeDrunk()
    {
        var __reg2 = this.pointsA.length / 2;
        if ((this.pointNum < __reg2 && this.walkSpeed > 0) || (this.pointNum > __reg2 && this.walkSpeed < 0)) 
        {
            this.walkSpeed = this.walkSpeed * -1;
            this._xscale = this._xscale * -1;
        }
        this.speedFactor = 0.5;
        if (this.defFrame != "drunk") 
        {
            SoundManager.startSound("notawarmbeer" + Math.floor(Math.random() * 2));
        }
        this.defFrame = "drunk";
    }

    function doShakespear()
    {
        this.speedFactor = 0;
        this.defFrame = "shakespear";
        this.gotoAndStop("shakespear");
    }

    function stopShakespear()
    {
        this.speedFactor = 1;
        this.defFrame = "def";
        this.gotoAndStop("running");
    }

}
